﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Web;
using System.Web.Security;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Exceptions;
using AutoPartsSearch.Domain.Entities.Enums;

namespace AutoPartsSearch.Web.Infrastructure
{
    public class AspNetMembership : IMembership
    {
        public bool ValidateUser(string userName, string password)
        {
            return Membership.ValidateUser(userName, password);
        }

        public void SetCookie(string userName, bool remember)
        {
            FormsAuthentication.SetAuthCookie(userName, remember);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        public void CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus createStatus;
            Membership.CreateUser(userName, password, email, null, null, true, null, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
                return;
            throw new CreateUserFailedException(ErrorCodeToString(createStatus));
        }

        public void AddRoleToUser(string userName, string role)
        {
            Roles.AddUserToRole(userName, role);
        }

        public bool IsUserInRole(string userName, string role)
        {
            return Roles.IsUserInRole(userName, role);
        }

        public string GetUserNameByEmail(string email)
        {
            return Membership.GetUserNameByEmail(email);
        }

        public string ResetPassword(string userName)
        {
            if (!Membership.EnablePasswordReset)
            {
                throw new Exception("Password reset is not enabled.");
            }
            var currentUser = Membership.GetUser(userName);
            if (currentUser == null)
            {
                return null;
            }
            return currentUser.ResetPassword();
        }

        public string GetCurrentUserName()
        {
            throw new NotImplementedException();
        }

        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "Пользователь с таким именем уже существует. Введите пожалуйста другое.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "Пользователь с таким e-mail уже существует. Пожалуйста, введите другой e-mail.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
    }
}